/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package retrieval;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import queryterm.QueryTerm;
import util.FileListing;

/**
 *
 * @author Danang
 */
public class QueryTermPositionPerField {
    HashMap<Integer,QueryTermPosition> listQueryTerms;
    String token;
    
    public QueryTermPositionPerField(String token) {
        listQueryTerms = new HashMap<Integer, QueryTermPosition>();
        this.token = token;
    }
    
    public void addQueryListPosition(int field, ArrayList<Integer> a) {
        QueryTermPosition qt;
        if (!listQueryTerms.containsKey(field)) {
            qt = new QueryTermPosition();
            listQueryTerms.put(field, qt);
        } else {
            qt = listQueryTerms.get(field);
        }
        qt.setListPosition(a);
    }
    
    public int getQueryTf(int field) {
        if (!listQueryTerms.containsKey(field)) {
            return 0;
        }
        return listQueryTerms.get(field).getTf();
    }
    
    public ArrayList<Integer> getQueryPosition(int field) {
        return listQueryTerms.get(field).getListPosition();
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(token).append(FileListing.newLine);
        Iterator iter = listQueryTerms.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry pairs = (Map.Entry) iter.next();
            int field = (Integer) pairs.getKey();
            QueryTermPosition qt = (QueryTermPosition) pairs.getValue();
            sb.append("Field: ").append(QueryTerm.getFieldString(field)).append(" ").append(qt.toString());
        }
        
        return sb.toString();
    }
    
}
